package com.itheima.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.core.controller.BaseController;
import com.itheima.common.core.domain.R;
import com.itheima.common.core.page.TableDataInfo;
import com.itheima.server.domain.Business;
import com.itheima.server.domain.BusinessCommunication;
import com.itheima.server.domain.BusinessDetail;
import com.itheima.server.dto.BusinessFollowDto;
import com.itheima.server.dto.BusinessModDto;
import com.itheima.server.dto.BusinessPageQueryDto;
import com.itheima.server.service.BusinessCommunicationService;
import com.itheima.server.service.BusinessDetailService;
import com.itheima.server.service.IBusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/business")
public class BusinessController extends BaseController {
    @Autowired
    private IBusinessService businessService;
    @Autowired
    private BusinessCommunicationService businessCommunicationService;
    @Autowired
    private BusinessDetailService businessDetailService;


    /**
     * 新增商机
     *
     * @param business
     * @return
     */
    @PostMapping
    public R add(@RequestBody Business business) {
        log.info("新增商机,参数：{}", business);
        R  result = businessService.addBussiness(business);
        return result;
    }


    /**
     * 分页查询商机
     *
     * @param businessPageQueryDto
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(BusinessPageQueryDto businessPageQueryDto) {
        log.info("查询所有商机,参数：{}", businessPageQueryDto);

        startPage();
        List<Business> list = businessService.selectBusinessList(businessPageQueryDto);
        return getDataTable(list);
    }

    /**
     * 分页查询公海池
     *
     * @param businessPageQueryDto
     * @return
     */
    @GetMapping("/pool")
    public TableDataInfo listPool(BusinessPageQueryDto businessPageQueryDto) {
        log.info("查询所有公海池数据,参数：{}", businessPageQueryDto);
        startPage();
        List<Business> list = businessService.selectPoolList(businessPageQueryDto);
        return getDataTable(list);
    }

    /**
     * 根据id查询商机
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        log.info("查询商机,参数：{}", id);

        BusinessDetail businessDetail = businessDetailService.getById(id);
        if (businessDetail != null){
            return R.ok(businessDetail);
        }

        Business business = businessService.getById(id);
        return R.ok(business);
    }

    /**
     * 查询沟通记录
     *
     * @param businessId
     * @return
     */
    @GetMapping("/record/list")
    public R ListRecord(Integer businessId) {
        log.info("查询沟通记录,客户id：{}", businessId);
        LambdaQueryWrapper<BusinessCommunication> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BusinessCommunication::getBusinessId, businessId);
        List<BusinessCommunication> list = businessCommunicationService.list(wrapper);
        return R.ok(list);
    }


    /**
     * 批量捞取商机
     *
     * @param businessGainDto
     * @return
     */
    @PutMapping("/gain")
    public R batchGain(@RequestBody BusinessModDto businessGainDto) {
        log.info("商机捞取,ids：{},userId：{}", businessGainDto.getIds(), businessGainDto.getUserId());
        R result = businessService.batchGain(businessGainDto.getIds(), businessGainDto.getUserId());
        return result;
    }

    /**
     * 商机跟进
     *
     * @param businessFollowDto
     * @return
     */
    @PostMapping("/record")
    public R followBusiness(@RequestBody BusinessFollowDto businessFollowDto) {
        log.info("添加商机沟通记录,参数：{}", businessFollowDto);
        Boolean result = businessDetailService.followBusiness(businessFollowDto);
        return R.ok(result);
    }

    /**
     * 商机分配
     * @param businessModDto
     * @return
     */
    @PutMapping("/assignment")
    public R assignment(@RequestBody BusinessModDto businessModDto){
        log.info("商机分配,ids：{},userId：{}", businessModDto.getIds(), businessModDto.getUserId());
        R result = businessService.batchAssignment(businessModDto.getIds(), businessModDto.getUserId());
        return result;
    }

    /**
     * 商机踢回公海
     * @param businessId
     * @param reason
     * @return
     */
    @PutMapping("/back/{businessId}/{reason}")
    public R back(@PathVariable Integer businessId,@PathVariable String reason){
        log.info("商机踢回公海池,id：{},reason：{}", businessId, reason);
        R result = businessService.back(businessId, reason);
        return result;

    }



}




